package com.sinodata.bsm.cicp.finder.aix;

/**
 * AIX Memory发现程序.
 * 程序返回：内存页大小，内存大小
 * 其中：aix 的内存页大小现在采不到，在这里使用固定值 4096 byte
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

public class Mem extends AixFinder {
    private static final Logger logger = Logger.getLogger(Mem.class);
    private static final String CMDSTR = "lsattr -El sys0 -a realmem";
    private static final int K = 1024;

    public static void main(String[] args) {
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] memRes = null;
        BufferedReader reader = null;
        String strLine = null;
        String regex = "realmem\\s+(\\d+).*";
        Pattern pattern = Pattern.compile(regex);
        try {
            reader = CommandReader.getReader(ip, CMDSTR, 0, 0);
            while ((strLine = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(strLine);
                if (matcher.find()) {
                    memRes = new String[5];
                    list.add(memRes);
                    memRes[0] = "0";
                    memRes[1] = "Memory";
                    memRes[2] = getHostName(ip) + " Memory";
                    memRes[3] = "4096";//内存页大小
                    memRes[4] = Integer.parseInt(matcher.group(1)) / K + "";//内存大小,单位是Mb
                }
            }
            return list;
        } catch (IOException e) {
            logger.error("failed to find aix memory", e);
        }
        return null;
    }
}
